package org.tobuy.servlet;


import java.io.IOException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tobuy.factory.AuctionDAOFactory;
import org.tobuy.factory.ItemDAOFactory;
import org.tobuy.vo.*;


public class ItemEstablishServlet extends HttpServlet {



	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		User user = (User)req.getSession().getAttribute("user");
		String path = "establish_pass.jsp";
		String itemtype = req.getParameter("type");
		String itemname = req.getParameter("itemname");
		double price = Double.parseDouble(req.getParameter("price"));
		String description = req.getParameter("description");
		int amount = Integer.parseInt(req.getParameter("amount"));
		String sellerid = user.getUser_id();
		String a1 = req.getParameter("a1");
		String a2 = req.getParameter("a2");
		String a3 = req.getParameter("a3");
		String a4 = req.getParameter("a4");
		System.out.println("*********************");
		String[] auc = new String[1];
		auc[0]="0";
		if(req.getParameter("auction")!=null){
			auc[0]="1";
		}
		int last_insert_id=-1;
		int hour = Integer.parseInt(req.getParameter("hour"));
		int min = Integer.parseInt(req.getParameter("min"));
		Timestamp exp_time = new Timestamp(System.currentTimeMillis());
		exp_time.setHours(exp_time.getHours()+hour);
		exp_time.setMinutes(exp_time.getMinutes()+min);
		
		
		String[] info = new String[3];  // collect information
		if(itemtype==null || "".equals(itemtype)){
			info[0]="All the information must be filled!";
		}
		if(itemname==null || "".equals(itemname)){
			info[0]="All the information must be filled!";
		}
		if(req.getParameter("price")==null || "".equals(req.getParameter("price"))){
			info[0]="All the information must be filled!";
		}
		if(description==null || "".equals(description)){
			info[0]="All the information must be filled!";
		}
		if(req.getParameter("amount")==null || "".equals(req.getParameter("amount"))){
			info[0]="All the information must be filled!";
		}

		if(sellerid==null || "".equals(sellerid)){
			info[0]="All the information must be filled!";
		}
		if(a1==null || "".equals(a1)){
			info[0]="All the information must be filled!";
		}
		if(a2==null || "".equals(a2)){
			info[0]="All the information must be filled!";
		}
		if(a3==null || "".equals(a3)){
			info[0]="All the information must be filled!";
		}
		if(a4==null || "".equals(a4)){
			info[0]="All the information must be filled!";
		}
		if(info[0]==null){  // no mistake

			if("book".equals(itemtype)){
				Book book = new Book();
				book.setType(itemtype);
				book.setItem_name(itemname);
				book.setPrice(price);
				book.setBeschreibung(description);
				book.setAmount(amount);
				book.setSeller_id(sellerid);
				book.setIsbn(a1);
				book.setVerlag(a2);
				book.setAutor(a3);
				book.setGebunden(a4);
				
				try{
					last_insert_id = ItemDAOFactory.getIItemDAOInstance().addItem(book, user);
					System.out.println(last_insert_id);
					if(last_insert_id!=-1){
						info[1]="Successful!";
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if("game".equals(itemtype)){
				Game game = new Game();
				game.setType(itemtype);
				game.setItem_name(itemname);
				game.setPrice(price);
				game.setBeschreibung(description);
				game.setAmount(amount);
				game.setSeller_id(sellerid);
				game.setPlatteform(a1);
				game.setAge_restriction(a2);
				game.setProducer(a3);
				game.setCatigory(a4);
				try{
					last_insert_id = ItemDAOFactory.getIItemDAOInstance().addItem(game, user);
					System.out.println(last_insert_id);
					if(last_insert_id!=-1){
						info[1]="Successful!";
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if("dvd".equals(itemtype)){
				Dvd dvd = new Dvd();
				dvd.setType(itemtype);
				dvd.setItem_name(itemname);
				dvd.setPrice(price);
				dvd.setBeschreibung(description);
				dvd.setAmount(amount);
				dvd.setSeller_id(sellerid);
				dvd.setPlay_time(a1);
				dvd.setRegion(a2);
				dvd.setProducer(a3);
				dvd.setAge_restriction(a4);
				try{
					last_insert_id = ItemDAOFactory.getIItemDAOInstance().addItem(dvd, user);
					System.out.println(last_insert_id);
					if(last_insert_id!=-1){
						info[1]="Successful!";
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			
		}
		if("1".equals(auc[0])&&last_insert_id!=-1){
			Auction auction = new Auction();
			auction.setItem_id(last_insert_id);
			auction.setSeller_id(sellerid);
			auction.setExp_time(exp_time);
			try {
				if(AuctionDAOFactory.getIAuctionDAOInstance().addAuction(auction)){
					info[2]="Auction created!";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		req.setAttribute("info", info);
		req.getRequestDispatcher(path).forward(req, resp);
	}


	
	
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);

		
	}


}
